<template>
  <div class="doctx">
    <div class="exam-left">
      <div class="exam-tool">
        <h3 class="tool-title">试卷详情</h3>
        {{ examPaper.courseExam?.fullScore }}
        <div class="bor">
          <span>总分：{{ examPaper.courseExam?.fullScore }}</span>
          <br />
          <span>时长：{{ examPaper.courseExam?.durationTime/60 }}分钟</span>
        </div>

        <div class="bor">
          <span :key="item.id" v-for="item in examPaper.courseExamPaperQuestionList">
            <el-tag class="do-exam-title-tag"
                @click="goAnchor('#question-' + item.id)">{{ item.questionNumber }}</el-tag>
          </span>
        </div>
        <div class="bor center">
          剩余时间
          <p class="date">{{ formatSeconds(remainTime) }}</p>
        </div>
        <div class="center" style="margin-top: 10px">
          <el-button size="small" type="primary" @click="submitForm">提交试卷</el-button>
        </div>
      </div>
    </div>
    <div class="exam-ctx">
      <el-form :model="form" ref="form" v-loading="formLoading" label-width="100px">
        <div :key="index" v-for="(question, index) in examPaper.courseExamPaperQuestionList"
             :id="'question-' + question.id">
          <div class="exam-paper-title">{{question.questionNumber}}、{{ question.courseExamQuestion.title }}</div>
          <div class="exam-paper-item-box">
            <el-form-item :key="option.id"
                v-for="option in question.courseExamQuestion.courseExamQuestionOptionList" class="exam-question-item" label-width="50px">
              <div style="line-height: 1.8">
                  <div v-if="question.courseExamQuestion.questionType === 1">
                    <div class="q-content">
                      <el-radio-group v-model="question.answerContent" @change="selectOption(question)">
                        <el-radio class="option-item" :label="option.number" :value="option.number">
                          {{option.number}}、<span v-html="option.title" class="q-item-span-content"></span>
                        </el-radio>
                      </el-radio-group>
                    </div>
                  </div>
                  <div v-else-if="question.courseExamQuestion.questionType === 2">
                    <div class="q-content">
                      <el-checkbox-group class="option-item" v-model="question.answerContentArray" @change="selectOption(question)">
                        <el-checkbox :label="option.number" :value="option.number">
                          <span class="question-prefix">{{ option.number }}.</span>
                          <span v-html="option.title" class="q-item-span-content"></span>
                        </el-checkbox>
                      </el-checkbox-group>
                    </div>
                  </div>
                  <div v-if="question.courseExamQuestion.questionType === 3" @change="selectOption(question)">
                    <div class="q-content">
                      <el-radio-group v-model="question.answerContent">
                        <el-radio class="option-item" :label="option.number" :value="option.number">
                          {{option.number}}、<span v-html="option.title" class="q-item-span-content"></span>
                        </el-radio>
                      </el-radio-group>
                    </div>
                  </div>
              </div>
            </el-form-item>
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getPaperDetail,startAnswerQuestion,submitPaper } from "@/api/u/exam";
import { formatSeconds } from "@/util/dateUtils"
export default {
  components: {
  },
  data() {
    return {
      examPaper: {},
      formLoading: false,
      form: {},
      radio1: '',
      remainTime: 0,
      timer: null
    };
  },
  created() {
    this.getPaperDetail();
    this.timeReduce();
  },
  mounted() {},
  methods: {
    // 格式化倒计时
    formatSeconds(theTime) {
      return formatSeconds(theTime);
    },
    // 倒计时
    timeReduce() {
      let _this = this
      _this.timer = setInterval(function () {
        if (_this.remainTime < 0) {
          _this.submitForm();
        } else {
          --_this.remainTime;
        }
      }, 1000);
    },
    // 获取试卷详细信息
    getPaperDetail(){
      this.formLoading = true;
      let param = {
        paperId: this.$route.query.id
      }
      getPaperDetail(param).then((res) => {
        if (res.code === 200) {
          this.examPaper = res.data;
          this.formLoading = false;
          // 计算考试倒计时
          let remainTimeA = new Date(this.examPaper.createTime).getTime()/100 + this.examPaper.courseExam.durationTime
          let nowTime = new Date().getTime()/100
          this.remainTime = remainTimeA - nowTime
        } else {
          this.$message.error("该试卷不存在");
        }
      });
    },
    // 选中选项
    selectOption(question){
      console.log(question.answerContentArray)
      let param = {
        paperQuestionId: question.id,
        answerContent: question.courseExamQuestion.questionType===2?question.answerContentArray.join(","):question.answerContent
      }
      startAnswerQuestion(param).then((res) => {
        if (res.code !== 200) {
          this.$message.error("答题失败");
        }
      });
    },
    // 提交试卷
    submitForm(){
      let _this = this
      let param = {
        paperId: _this.$route.query.id
      }
      submitPaper(param).then((res) => {
        if (res.code === 200) {
          _this.$message.success("交卷成功");
        }
        _this.$router.push(`/courseDetail?id=${_this.examPaper.courseExam.courseId}`);
        window.clearInterval(_this.timer);
      });
    },
    // 跳转到对应题目
    goAnchor(selector){
      this.$el.querySelector(selector).scrollIntoView({
        behavior: "instant",
        block: "center",
        inline: "nearest",
      });
    }
  },
  computed: {
  },
};
</script>

<style lang="scss" scoped>
.align-center {
  text-align: center;
}

.exam-question-item {
  padding: 10px;

  .el-form-item__label {
    font-size: 15px !important;
  }
}

.question-title-padding {
  padding-left: 25px;
  padding-right: 25px;
}

.doctx {
  position: relative;
  padding: 20px 100px;
  display: flex;
  flex-direction: row;
  background: #eff3f7;

  .exam-tool {
    border: 1px solid #e4e7ed;
    border-radius: 5px;
    overflow: hidden;
    width: 260px;
    position: fixed;
    min-height: 95%;
    padding: 25px 15px;
    box-sizing: border-box;
    background: #fff;
    .tool-title {
      font-size: 20px;
      font-weight: 300;
      margin-bottom: 10px;
    }
    .bor {
      padding: 15px 0;
      border-bottom: 1px solid gainsboro;
    }
    .center {
      text-align: center;
    }
    .date {
      font-size: 18px;
      color: orangered;
      margin-top: 8px;
    }
  }
  .exam-left {
    width: 260px;
    margin-right: 20px;
  }
  .exam-ctx {
    border: 1px solid #e4e7ed;
    flex: 1 1 0%;
    background: red;
    min-height: 1000px;
    background: #ffff;
    .exam-paper-title {
      font-size: 18px;
      line-height: 25px;
      padding: 18px 20px;
      background: #fafafa;
      border-top: 1px solid #dedede;
      border-bottom: 1px solid #dedede;
    }
    .exam-paper-item-box {
      padding: 30px;
      font-size: 14px;
      color: #4c596b;
    }
  }

  // .exam-ctx {
  //   margin-left: 400px;
  //   width: 65%;
  //   background: rebeccapurple;
  // }
  // .exam-tool {
  //   width: 100px;
  //   min-height: 500px;
  //   position: fixed;
  //   top: 20px;
  //   background: red;
  //   margin-top: 50px;
  // }
}

.do-exam-title-tag {
  margin-left: 5px;
  margin-bottom: 5px;
  cursor: pointer;
}
</style>